home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / dino / dino_bot.1 / examples / complex / mm.d < prev    next >
Encoding:
Text File  |  1991-03-10  |  3.3 KB  |  101 lines

  1. /* Copyright, 1990, Regents of the University of Colorado */
  2. /* This program does a matrix-matrix multiply, of two matrices of size
  3.  * N x N, where N is a multiple of P, the number of environments.
  4.  *
  5.  * The basic algorithm is to block the first matrix
  6.  * by rows and send a block to each processor, and
  7.  * block the second matrix by columns and send a block
  8.  * to each processor.  Each processor computes the
  9.  * results for that sub-matrix for which it has data
  10.  * and then the blocks of columns are all shifted to
  11.  * the right one processor. */
  12.  
  13. #include "dino.h"
  14.  
  15. #define N 32
  16. #define P 16
  17.  
  18. environment node[P:id] {
  19.  
  20.   composite mult (in A, in B, out C)
  21.   float distributed A[N][N] map BlockRow;       /* First matrix */
  22.   float distributed B[N][N] map BlockCol;       /* Second matrix */
  23.   float distributed C[N][N] map BlockRow;       /* Result matrix */
  24.  
  25.   {
  26.     int i, j, k, l;             /* Looping variables */
  27.     int my_first, my_last;      /* Columns of B which are mine */
  28.     int left_first, left_last;  /* Columns of B which are my left neighbor's */
  29.     int left, right;            /* Environment indices of my neighbors */
  30.  
  31.     /* Compute the environment indices of the nodes on the right and left */
  32.     right = (id == P - 1) ? (0) : (id + 1);
  33.     left = (id == 0) ? (P - 1) : (id - 1);
  34.  
  35.     /* Compute the starting and stopping indices of the data in my block */
  36.     my_first = id * N/P;
  37.     my_last = (id + 1) * N/P - 1;
  38.  
  39.     /* Compute the starting and stopping indices of the data in the block
  40.      * of the node to the left */
  41.     left_first = (id == 0) ? ((P - 1) * N/P) : ((id - 1) * N/P);
  42.     left_last = (id == 0) ? (N - 1) : (id * N/P - 1);
  43.  
  44.     /* Loop through the blocks of columns */
  45.     for (i = 0; i < P; i++) {
  46.  
  47.       /* First, send out the data that the node on the right of me will
  48.        * need for the next iteration */
  49.       B[][<left_first, left_last>]# {node[left]} =
  50.                      B[][<my_first, my_last>];
  51.  
  52.       /* Loop thru the block of C[][] that I currently have data for */
  53.       for (j = my_first; j < my_last + 1; j++)
  54.         for (k = 0; k < N/P; k++) {
  55.  
  56.           /* Compute the dot product of the appropriate data */
  57.           C[j][k + ((id + i) * N/P) % N] = 0;
  58.           for (l = 0; l < N; l++)
  59.             C[j][k + ((id + i) * N/P) % N] +=
  60.                      A[j][l] * B[l][k + my_first];
  61.         }
  62.  
  63.       /* Finally, receive the data from the node on the right that I need
  64.        * for the next iteration */
  65.       B[][<my_first, my_last>] = B[][<my_first, my_last>]# {node[right]};
  66.     }
  67.   }
  68. }
  69.  
  70. environment host {
  71.  
  72.   main() {
  73.  
  74.     int i, j;                           /* Looping variables */
  75.     float a[N][N], b[N][N], c[N][N];
  76.  
  77.     /* Initialize the two multiplicand arrays */
  78.     printf ("Initializing...\n");
  79.     for (i = 0; i < N; i++)
  80.        for (j = 0; j < N; j++) {
  81.          a[i][j] = 0.1;
  82.          b[i][j] = (i + j) / 10.0;
  83.        }
  84.  
  85.     /* Preform the computation */
  86.     printf ("Performing the computation...\n");
  87.     mult(a[][], b[][], c[][])#;
  88.  
  89.     /* Print out the results -- because the data we used for a[][] and b[][]
  90.      * generates the same results for each row, we only print out the first
  91.      * row */
  92.  
  93.     printf("Results:\n");
  94.     for (i = 0; i < N; i++) {
  95.       if (i % 8 == 0) printf("\n");
  96.       printf("   %6.2f", c[0][i]);
  97.     }
  98.     printf("\n");
  99.   }
  100. }
  101.